package com.ea.eadp.nexus.test.pageDefinition.originX;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class UnderageEmailVerificationPage extends BasePage {

    private static final String TITLE_ID = "loginUnderageVerification";
    private static final String CONTINUE_BUTTON_ID = "submit";
    private static final String RESEND_LINK_ID = "resend";
    private static final String ERROR_MESSAGE_PATH = "//section[@id='loginUnderageVerification']/p";
    private static final String EMAIL_SENT_PATH = "//section[@id='loginUnderageVerification']/p[2]/a[@class='otka']";
    private static final String EMAIL_UPDATED_PATH = "//section[@id='loginUnderageVerification']/p";
    private static final String UPDATE_EMAIL_ID = "update";
    private static final String PARENT_EMAIL_UPDATE_FORM_ID = "parentEmailUpdateForm";
    private static final String PARENT_EMAIL_TEXTBOX_ID = "parentEmail";

    private static final String ERROR_MESSAGE = "Email has not been verified\nWe sent an email to your parent/guardian at\n%s";
    private static final String EMAIL_SENT_TEXT = "Email sent";
    private static final String EMAIL_UPDATED_TEXT = "We sent an email to your parent/guardian at\n%s";

    public UnderageEmailVerificationPage(final WebDriver driver) {
        super(driver, UnderageEmailVerificationPage.class);
    }

    public void waitPageLoadCompleted() {
        PageOperation.WebDriverWait(driver, TITLE_ID);
    }

    public void waitParentEmailUpdatePageLoadCompleted() {
        PageOperation.WebDriverWait(driver, PARENT_EMAIL_UPDATE_FORM_ID);
    }

    public void clickResendEmail() {
        driver.findElement(By.id(RESEND_LINK_ID)).click();
    }

    public void clickUpdateEmailLink() {
        driver.findElement(By.id(UPDATE_EMAIL_ID)).click();
    }

    public void updateEmail(final String parentEmail) {
        PageOperation.SendKeyToElementAfterWait(driver, PARENT_EMAIL_TEXTBOX_ID, parentEmail);
    }

    public void clickContinueButton() {
        driver.findElement(By.id(CONTINUE_BUTTON_ID)).click();
    }

    public void validateErrorMessage(final String parentEmal) {
        driver.findElement(By.xpath(ERROR_MESSAGE_PATH));
        Assert.assertEquals(driver.findElement(By.xpath(ERROR_MESSAGE_PATH)).getText(), String.format(ERROR_MESSAGE, parentEmal));
    }

    public void validateEmailSent() {
        PageOperation.WebDriverWaitByXpath(driver, EMAIL_SENT_PATH);
        Assert.assertEquals(driver.findElement(By.xpath(EMAIL_SENT_PATH)).getText(), EMAIL_SENT_TEXT);
    }

    public void validateEmailUpdated(final String newParentEmail) {
        PageOperation.WebDriverWaitByXpath(driver, EMAIL_UPDATED_PATH);
        Assert.assertEquals(driver.findElement(By.xpath(EMAIL_UPDATED_PATH)).getText(), String.format(EMAIL_UPDATED_TEXT, newParentEmail));
    }
}
